package com.kxmall.web.controller.riderHome;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.core.controller.BaseRiderController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.huishou.domain.bo.HsAppointmentOrderBo;
import com.kxmall.huishou.domain.vo.HsAppointmentOrderVo;
import com.kxmall.huishou.service.IHsAppointmentOrderService;
import com.kxmall.huishou.service.IHsSettlementOrderService;
import com.kxmall.rider.domain.vo.HsRiderStatisticsVo;
import com.kxmall.system.domain.SysNotice;
import com.kxmall.system.mapper.SysNoticeMapper;
import com.kxmall.web.controller.riderNotice.service.IRiderNoticetService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 公告
 *
 * @author kxmall
 * @date 2023-04-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/home/riderApp")
public class RiderHomeController extends BaseRiderController {

    private final IRiderNoticetService iRiderNoticetService;
    private final SysNoticeMapper sysNoticeMapper;
    private final IHsAppointmentOrderService iHsAppointmentOrderService;
    private final IHsSettlementOrderService iHsSettlementOrderService;


    /**
     * 查询首页统计信息
     */
    @GetMapping("/statistics")
    public R<Map<String, Object>> statistics() {

        Map<String, Object> dataResult = new HashMap<>();

        //获取首页通知
        Long userId = getRiderLoginUser().getUserId();
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(q->q.eq("user_id",userId).or().isNull("user_id"));
        queryWrapper.in("user_type", 0,2);
        queryWrapper.eq("status",0);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("LIMIT 10");
        List<SysNotice> sysNotices = sysNoticeMapper.selectList(queryWrapper);
        dataResult.put("sysNotice", sysNotices);

        //计算首页统计
        HsRiderStatisticsVo hsRiderStatisticsVo = new HsRiderStatisticsVo();
        hsRiderStatisticsVo.setRiderId(userId);
        hsRiderStatisticsVo.setId(1l);
        //获取未完成总订单数据
        List<Long> inCompleteStatus = new ArrayList<Long>();
        inCompleteStatus.add(2l);
        inCompleteStatus.add(4l);
        Long IncompleteOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,inCompleteStatus,null, null);
        hsRiderStatisticsVo.setIncompleteOrderNum(IncompleteOrderNum);

        //获取已完成订单
        List<Long> completeStatus = new ArrayList<Long>();
        completeStatus.add(5l);
        completeStatus.add(6l);
        Long completeOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,completeStatus,null, null);
        hsRiderStatisticsVo.setCompleteOrderNum(completeOrderNum);

        //获取失败订单
        List<Long> failStatus = new ArrayList<Long>();
        failStatus.add(7l);
        failStatus.add(8l);
        Long failOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,failStatus,null, null);
        hsRiderStatisticsVo.setFailOrderNum(failOrderNum);

        //获取昨日未完成订单
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(System.currentTimeMillis() - 24l*3600l*1000l));
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date yesterDay = cal.getTime();

        Calendar calNow = Calendar.getInstance();
        calNow.setTime(new Date());
        calNow.set(Calendar.HOUR_OF_DAY, 0);
        calNow.set(Calendar.MINUTE, 0);
        calNow.set(Calendar.SECOND, 0);
        calNow.set(Calendar.MILLISECOND, 0);
        Date today = cal.getTime();

        Long yesterdayOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,inCompleteStatus,yesterDay, today);
        hsRiderStatisticsVo.setYesterdayIncompleteOrderNum(yesterdayOrderNum);

        //获取今日未完成订单数
        Long todayOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,inCompleteStatus,today, null);
        hsRiderStatisticsVo.setTodayIncompleteOrderNum(todayOrderNum);

        //计算未接单的订单数
        List<Long> noTakingStatus = new ArrayList<Long>();
        noTakingStatus.add(0l);
        Long noTakeOrderNum = iHsAppointmentOrderService.countOrderByRiderId(null,noTakingStatus,null, null);
        hsRiderStatisticsVo.setNoTakeOrderNum(noTakeOrderNum);

        //待服务的订单数
        List<Long> waitingStatus = new ArrayList<Long>();
        waitingStatus.add(2l);
        Long waitingOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,waitingStatus,null, null);
        hsRiderStatisticsVo.setWaitServiceOrderNum(waitingOrderNum);

        //服务中的订单数
        List<Long> servicingStatus = new ArrayList<Long>();
        servicingStatus.add(4l);
        Long seiviceOrderNum = iHsAppointmentOrderService.countOrderByRiderId(userId,servicingStatus,null, null);
        hsRiderStatisticsVo.setInServiceOrderNum(seiviceOrderNum);

        dataResult.put("hsRiderStatistics", hsRiderStatisticsVo);

        //首页待服务订单列表
        HsAppointmentOrderBo hsAppointmentOrderBo = new HsAppointmentOrderBo();
        hsAppointmentOrderBo.setRiderId(userId);
        hsAppointmentOrderBo.setStatus(2l);
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(5);
        pageQuery.setIsAsc("desc");
        pageQuery.setOrderByColumn("create_time");
        TableDataInfo<HsAppointmentOrderVo> dataList = iHsAppointmentOrderService.queryPageListByStatus(hsAppointmentOrderBo,pageQuery);

        List<HsAppointmentOrderVo> appointmentOrderVoList = dataList.getRows();

        dataResult.put("appointmentOrderVoList", appointmentOrderVoList);

        return R.ok(dataResult);
    }


    /**
     * 查询我的统计
     */
    @GetMapping("/personStatistics")
    public R<Map<String, Object>> personStatistics() {

        Long riderId = getUserId();

        Map<String, Object> dataResult = new HashMap<>();
        //统计工作时长
        Long minutes = iHsAppointmentOrderService.countMinuteByRiderId(riderId);
        String timeStr = "0h";
        if(minutes != null || minutes.longValue() > 0l){
            if(minutes.longValue() < 60l){
                timeStr = minutes+"分钟";
            }else {
                BigDecimal hours = new BigDecimal(minutes).divide(new BigDecimal(60), 1,BigDecimal.ROUND_DOWN);
                timeStr = hours+"h";
            }
        }
        dataResult.put("workHours",timeStr);
        //统计接单记录数
        Long orderNum = iHsAppointmentOrderService.countOrderByRiderId(riderId,null,null,null);
        if(orderNum == null){
            orderNum = 0l;
        }
        dataResult.put("orderNum",orderNum);
        //当日流水
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Double balanceFlowDay = iHsSettlementOrderService.sumAmountByRiderId(riderId, cal.getTime(), null);

        dataResult.put("balanceFlowDay",balanceFlowDay);
        //累积流水
        Double balanceFlowTotal = iHsSettlementOrderService.sumAmountByRiderId(riderId, null, null);
        dataResult.put("balanceFlowTotal",balanceFlowTotal);


        return R.ok(dataResult);
    }

}
